(* parse.ml *)
open Lexing

let print_position outx lexbuf =
  let pos = lexbuf.lex_curr_p in
  Printf.fprintf outx "%s:%d:%d" pos.pos_fname
    pos.pos_lnum (pos.pos_cnum - pos.pos_bol + 1)

let parse_with_error lexbuf =
  try Parser.program Lexer.token lexbuf with
  | Lexer.Error msg ->
      Printf.fprintf stderr "%a: %s\n" print_position lexbuf msg;
      exit (-1)
  | Parser.SyntaxError msg ->
      Printf.fprintf stderr "%a: syntax error %s\n" print_position lexbuf msg;
      exit (-1)

let parse_program lexbuf =
  parse_with_error lexbuf

let parse_file filename =
  let inx = open_in filename in
  let lexbuf = Lexing.from_channel inx in
  lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = filename };
  let ast = parse_program lexbuf in
  close_in inx;
  ast

let parse_string s =
  let lexbuf = Lexing.from_string s in
  parse_program lexbuf